package bdd.beans;

import java.sql.*;

public class DBLink {
	
	private Connection connection;
	private PreparedStatement pst;
	private ResultSet rs;
	private final String IP_SERVEUR = "miage03.dmiage.u-paris10.fr";
	private final String PORT_SERVEUR = "1521";
	private final String NOMBD_SERVEUR = "miage";
	private final String LOGIN_SERVEUR = "m12009";
	private final String MDP_SERVEUR = "m12009pw";
	
	public DBLink() { }
	
	public DBLink(Connection connection, PreparedStatement pst, ResultSet rs) {
		this.connection = connection;
	    this.pst = pst;
	    this.rs = rs;
	}
	
	public Connection getConnection() {
		return connection;
	}
	
	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	
	public PreparedStatement getPst() {
		return pst;
	}
	
	public void setPst(PreparedStatement pst) {
		this.pst = pst;
	}
	
	public ResultSet getRs() {
		return rs;
	}
	
	public void setRs(ResultSet rs) {
		this.rs = rs;
	}
	
	/**
	 * Etablit la connexion à la base de données
	 */
	public void connexion() {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		}
		catch (ClassNotFoundException cnfe) {
			System.out.println("Driver introuvable");
		}
		
		try {
			connection = DriverManager.getConnection("jdbc:oracle:thin:@//" + IP_SERVEUR + ":" + PORT_SERVEUR + "/" + NOMBD_SERVEUR, LOGIN_SERVEUR, MDP_SERVEUR);
		}
		catch (SQLException se) {
			System.out.println("Base de données introuvable ou erreur dans une requête");
		}
	}

        public boolean estReconnue(String login, String passwd) {
                boolean estReconnue = false;

                try {
                        pst = connection.prepareStatement("SELECT PWD FROM ENSEIGNANT WHERE login='"+login+"'");
                        //pst.setString(1, login);
                        rs = pst.executeQuery();

                        while(rs.next()) {
                                if(rs.getString("PWD").equals(passwd))
                                  estReconnue = true;
                        }
                }
                catch(SQLException se) {
                        System.out.println("Erreur de requête");
                }

                return estReconnue;
        }

	
	public void deconnexion() {
		try {
            //pst.close();
            connection.close();
        } catch (SQLException ex) {
            System.err.println("Erreur fermeture connexion");
        }
	}
}
